<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
use app\models\FeeItem;
/* @var $form yii\widgets\ActiveForm */
?>

<style>
    .col-sm-2 {
        width: 30%;
    }

    .control-label {
        text-align: left !important;
    }

    .col-md-10 {
        width: 65%;
    }

    table#other_fee_table th{
        padding-left:0px;
    }

    table#other_fee_table td{
        padding:0px 5px 0px 0px;
    }

    .col-md-offset-2 {
        margin-left:30%;
    }

    .form-group {
        margin-bottom: 5px;
    }

    .modal-header {
        padding: 0px 15px;
    }
</style>

<script>
    // 	function btnGroup(_this){
    // 		$("#customer-cust_type").val(_this.innerHTML);
    // 	}

    var other_fee_nums = 0;

    function getCurrentMonth(){
        var date  = new Date();

        var mm = date.getMonth() + 1;
        if(mm<10){mm = '0'+mm}

        return date.getFullYear() + "/" + mm;
    }

    $(function(){
        $(".btn-group > .btn").click(function(){
            $(this).addClass("active").siblings().removeClass("active");
            $("#customer-cust_type").val(this.innerHTML);
        });

        <?php foreach ($model -> feeItems as $item){ ?>
        add_tr("<?= $item -> item?>", <?= $item -> fee ?>, "<?= $item -> fee_month ?>");
        <?php }?>

        if($("[name='other_fee_tr']").length === 1){
            add_tr(1,0, getCurrentMonth());
        }

        init();
    });

    function init(){
        var cust_type = "<?= $model -> cust_type ?>";

        cust_type = cust_type==""?"法人":cust_type;

        $("#cust_type button").each(function(){
            if (this.innerHTML === cust_type){
                $(this).addClass("active");
            }
        });
    }

    var tr_index = 0;

    function add_tr(item, fee, fee_month){
        if(other_fee_nums < 10){
            other_fee_nums++;

            tr_index++;
            var tr_raw = $("#other_fee_table tbody tr:first").clone();

            $("a", tr_raw).click({index: tr_index}, function(event){

                if ($("#other_fee_table tbody tr").length == 2)
                    return;

                if(confirm("Do you want to delete ?"))
                {
                    other_fee_nums--;
                    $("#tr_" + event.data.index).remove();
                }

            });

            tr_raw.attr("id", "tr_" + tr_index);
            tr_raw.css("display", "");

            if(typeof(item) != "undefined"){
                $("[name='FeeItem[item][]']", tr_raw).val(item);
                $("[name='FeeItem[fee][]']", tr_raw).val(fee);
                $("[name='FeeItem[fee_month][]']", tr_raw).val(fee_month);
            }

            $("[name='FeeItem[fee_month][]']", tr_raw).kendoDatePicker({
                // defines the start view
                start: "year",
                depth: "year",
                format: "yyyy/MM"
            });

            $("#other_fee_table > tbody").append(tr_raw);
        }
        return false;
    }

    // serialize form, render response and close modal
    function submitForm($form) {
        alert(1);

        $.post(
                $form.attr("action"), // serialize Yii2 form
                $form.serialize()
            )
            .done(function(result) {
                $form.parent().html(result.message);
                $('#modal').modal('hide');
            })
            .fail(function() {
                console.log("server error");
                $form.replaceWith('<button class="newType">Fail</button>').fadeOut()
            });
        return false;
    }

</script>

<?php
$monthList = [];

for($i=1; $i<=12; $i++){
    //array_push($monthList, $i);
    $monthList[$i] = $i . Yii::t('app', 'Month');
}

$yearList = [
    '2014' => '2014' . Yii::t('app', 'Year'),
    '2013' => '2013' . Yii::t('app', 'Year'),
    '2012' => '2012' . Yii::t('app', 'Year')
];
?>

<div class="customer-form">
    <?php $form = ActiveForm::begin([
        'id'    => 'customer-form',
        'options' => ['class' => 'form-horizontal'],
        'fieldConfig' => [
            'template' => "{label}\n<div class=\"col-md-10\">{input}</div>\n<div class=\"col-md-offset-2 col-md-10\">{error}</div>",
            'labelOptions' => ['class' => 'col-sm-2 control-label'],
        ],
        'beforeSubmit' => 'submitForm',
    ]);

    $form -> successCssClass = '';
    ?>

    <?php if($model -> cust_id != ""){ ?>
        <div class="form-group field-customer-cust_id">
            <label for="customer-cust_id" class="col-sm-2 control-label"><?= Yii::t('app', 'Customer ID') ?></label>
            <div class="col-md-10 btn-group" id="cust_id">
                <?=$model -> cust_id ?>
            </div>
        </div>
    <?php } ?>

    <div class="form-group field-customer-cust_type">
        <label for="customer-cust_type" class="col-sm-2 control-label"><?= Yii::t('app', 'Corporation') ?>/<?= Yii::t('app', 'Individual') ?>/<?= Yii::t('app', 'Spot') ?></label>
        <div class="col-md-10 btn-group" id="cust_type">
            <button type="button" class="btn btn-default">法人</button>
            <button type="button" class="btn btn-default">個人</button>
            <button type="button" class="btn btn-default">スポット</button>
            <input type="hidden" name="Customer[cust_type]" id="customer-cust_type"></div>
        <div class="col-md-offset-2 col-md-10"><div class="help-block"></div>
        </div>
    </div>

    <?= $form->field($model, 'cust_name')->textInput(['maxlength' => 50]) ?>

    <?= $form->field($model, 'is_operating')->checkbox([], false) ?>

    <?= $form->field($model, 'address')->textInput(['maxlength' => 255]) ?>

    <?= $form->field($model, 'person_in_charge')->textInput(['maxlength' => 100]) ?>

    <?= $form->field($model, 'email')->textInput(['maxlength' => 100]) ?>

    <?= $form->field($model, 'tel')->textInput(['maxlength' => 50]) ?>

    <?= $form->field($model, 'ns_person')->textInput(['maxlength' => 50]) ?>

    <?= $form->field($model, 'advisory_fee')->textInput() ?>

    <div class="form-group">
        <label for="" class="col-sm-2 control-label"><?= Yii::t('app', 'Advisory fee period') ?></label>
        <div class="col-md-10 btn-group">

            <?= $form->field($model, 'advisory_from_year', ['template' => '{input}', 'options' => ['class'=>'', 'tag' => 'div', 'style' => 'width:100px; float:left;']])
                ->dropDownList($yearList, ['style' => '']) -> label(false)?>

            <?= $form->field($model, 'advisory_from_month', ['template' => '{input}', 'options' => ['class'=>'', 'tag' => 'div', 'style' => 'width:70px; float:left;']])
                ->dropDownList($monthList, ['style' => '']) -> label(false)?>
            <div class="col-md-1" style="margin-top:7px; margin-left:5px;">ー</div>
            <?= $form->field($model, 'advisory_to_year', ['template' => '{input}', 'options' => ['class'=>'', 'tag' => 'div', 'style' => 'width:100px; float:left;']])
                ->dropDownList($yearList, ['style' => '']) -> label(false)?>
            <?= $form->field($model, 'advisory_to_month', ['template' => '{input}', 'options' => ['class'=>'', 'tag' => 'div', 'style' => 'width:70px; float:left;']])
                ->dropDownList($monthList, ['style' => '']) -> label(false)?>
        </div>
        <div class="col-md-offset-2 col-md-10">
            <div class="help-block"></div></div>
    </div>

    <?= $form->field($model, 'close_fee')->textInput() ?>

    <?= $form->field($model, 'close_month')->dropDownList($monthList, ['style' => 'width:70px']) ?>

    <?= $form->field($model, 'close_compensation_month')->dropDownList($monthList, ['style' => 'width:70px']) ?>

    <?= $form->field($model, 'payroll_compensation')->textInput() ?>

    <?= $form->field($model, 'payroll_month')->dropDownList($monthList, ['style' => 'width:70px']) ?>

    <?= $form->field($model, 'payroll_compensation_month')->dropDownList($monthList, ['style' => 'width:70px']) ?>

    <div class="form-group">
        <label for="" class="col-sm-2 control-label"><?= Yii::t('app', 'Other fee') ?></label>
        <table id="other_fee_table">
            <thead>
            <th><?= Yii::t('app', 'Fee item') ?></th>
            <th><?= Yii::t('app', 'Fee') ?></th>
            <th><?= Yii::t('app', 'Fee month') ?></th>
            <th></th>
            </thead>
            <tbody>
            <tr name="other_fee_tr" style="display: none;">
                <td>
                    <select class="form-control" style="width:150px;" name="FeeItem[item][]">
                        <?php foreach( (new FeeItem()) -> getItemType() as $key => $value){?>
                            <option value="<?= $key ?>"><?= $value ?></option>
                        <?php }?>
                    </select>
                </td>
                <td>
                    <input type="number" class="form-control" name="FeeItem[fee][]" style="width:120px;">
                </td>
                <td>
                    <input class="form-control" style="width:120px;" name="FeeItem[fee_month][]">
                </td>
                <td><a><?= Yii::t('app', 'Delete') ?></a>
                </td>
            </tr>
            </tbody>
        </table>
        <div style="margin-left:250px;"><a  href="#" onclick="return add_tr();"><?= Yii::t('app', 'Add item') ?></a></div>
    </div>

    <?= $form->field($model, 'is_tax_form')->checkbox([], false)?>

    <?= $form->field($model, 'is_bookkeeping')->checkbox([], false) ?>

    <?= $form->field($model, 'is_labor_form')->checkbox([], false)?>

    <?= $form->field($model, 'is_legal_adjustment')->checkbox([], false)?>

    <div class="form-group field-customer-declaration_form">
        <label for="customer-declaration_form" class="col-sm-2 control-label"><?= Yii::t('app', 'Declaration form') ?></label>
        <div class="col-md-10 btn-group">
            <?php if($model -> declaration_form == ""){?>
                <input type="file" name="declaration_form" />
            <?php } else {?>
                <a href="/upload/customer/<?= $model -> declaration_form ?>"><?= Yii::t('app', 'Pdf file upload') ?></a>
            <?php }?>
        </div>
        <div class="col-md-offset-2 col-md-10">
            <div class="help-block"></div></div>
    </div>

    <div class="form-group field-customer-notification_form">
        <label for="customer-notification_form" class="col-sm-2 control-label"><?= Yii::t('app', 'Notification form') ?></label>
        <div class="col-md-10 btn-group">
            <?php if($model -> notification_form == ""){?>
                <input type="file" name="notification_form" />
            <?php } else {?>
                <a href="/<?= $model -> notification_form ?>"><?= Yii::t('app', 'Pdf file upload') ?></a>
            <?php }?>
        </div>
        <div class="col-md-offset-2 col-md-10">
            <div class="help-block"></div></div>
    </div>

    <?= $form->field($model, 'remark')->textArea(['maxlength' => 255]) ?>

    <div class="form-group" style="padding-left:10px;">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Signup') : Yii::t('app', 'Signup'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
